package com.app.onebuy.wxapi;







import com.app.onebuy.R;
import com.app.onebuy.basis.Constants;
import com.app.onebuy.user.ChargeActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import my.MySharedPreferences;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.);
        String appid = new MySharedPreferences(this).getString("wechat_appid");
        if(TextUtils.isEmpty(appid)){
        	appid = Constants.WECHAT_APPID_MAIN;
		}
    	api = WXAPIFactory.createWXAPI(this,appid);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp baseResp) {
		Log.d(TAG, "onPayFinish, errCode = " + baseResp.errCode);

		if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			EventBus.getDefault().post(new ChargeActivity.PayEvent(baseResp.errCode));
			finish();
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(baseResp.errCode)));
//			builder.show();
		}
	}

}